﻿using Core.Domain;
using Core.DomainServices;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.DomainServices
{
    public class ExportToWordV1 : IDocumentService
    {
        public readonly IDataRepository dataRepository;
        private readonly IAppConfig appConfig;


        public ExportToWordV1(IDataRepository dataRepository, IAppConfig appConfig)
        {
            this.dataRepository = dataRepository;
            this.appConfig = appConfig;
        }

        public IList<string> Export(string workingFolder)
        {
            string path = Directory.GetCurrentDirectory().Replace("bin" + Path.DirectorySeparatorChar + "Debug", "DataSample") + Path.DirectorySeparatorChar + "TemplateData.xlsx";
            var ds = dataRepository.ReadData(path);
            var invoices = new List<InvoiceInfo>();
            var productItems = new List<ProductItem>();
            var results = new List<string>();
            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables.Contains(Constants.Invoice.TABLE))
                {
                    DataTable table = ds.Tables[Constants.Invoice.TABLE];
                    foreach (DataRow row in table.Rows)
                    {
                        int invoiceNo = new Random().Next(1000, 4000);
                        var invoice = new InvoiceInfo(row);
                        invoice.InvoiceNo = invoiceNo.ToString();
                        invoices.Add(invoice);
                    }
                }

                if (ds.Tables.Contains(Constants.ProductItem.TABLE))
                {
                    DataTable table = ds.Tables[Constants.ProductItem.TABLE];
                    foreach (DataRow row in table.Rows)
                    {
                        productItems.Add(new ProductItem(row));
                    }
                }

                if (invoices.Any())
                {
                    var company = appConfig.GetXMLValues();
                    foreach (var item in invoices)
                    {
                        if (invoices.IndexOf(item) > 0)
                        {
                            var items = productItems.Where(d => d.OrderGUID == item.OrderGUID).ToList();
                            GeneratedInvoices generatedInvoices = new GeneratedInvoices();
                            generatedInvoices.Invoice = item;
                            generatedInvoices.ProductItems = items;
                            generatedInvoices.CompanyInformation = company;
                            string fileName = workingFolder + Path.DirectorySeparatorChar + generatedInvoices.Invoice.OrderGUID + ".docx";
                            Console.WriteLine(fileName);
                            generatedInvoices.CreatePackage(fileName);
                            results.Add(fileName);
                        }
                    }
                }

            }

            return results;
        }
    }
}
